﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Nirvana
{

    public class TextureReference
    {
        private Texture texture;
        private bool isRelease;
        ~TextureReference()
        {
            this.Release(true);
        }

        public void SetTexture(Texture texture)
        {
            if (this.texture == texture) return;
            if (this.texture != null)
            {
                this.Release(false);
            }

            this.texture = texture;
            Retain();
        }
        private void Retain()
        {
            if (this.texture != null && !this.isRelease)
            {
                this.isRelease = TexturePool.Instance.Retain(this.texture);
            }
        }

        private void Release(bool isrelease)
        {
            if (this.isRelease && this.texture != null)
            {
                this.isRelease = false;
                if (isrelease)
                {
                   // ReferenceDict.AddNeedClearTexture(this.texture);
                }
                else
                {
                    TexturePool.Instance.Free(this.texture, false);
                }

                this.texture = null;
            }
        }
    }
}
